/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.core.service.jdbc;

/**
 * 
 * @author 刘威
 */
import java.util.Date;
import java.util.ArrayList;

public class DBSet {

	int cursor = -1;
	ArrayList list = new ArrayList();

	public void addDBRow(DBRow row) {
		this.list.add(row);
	}

	public DBRow getDBRow(int index) {
		DBRow row = new DBRow();
		try {
			row = (DBRow) this.list.get(index);
		} catch (Exception ex) {
		}
		return row;
	}

	public DBRow getDBRow() {
		DBRow row = new DBRow();
		try {
			row = (DBRow) this.list.get(this.cursor);
		} catch (Exception ex) {
		}
		return row;
	}

	public boolean deleteDBRow() {
		boolean ret = false;
		try {
			this.list.remove(this.cursor);
			ret = true;
		} catch (Exception ex) {
		}
		return ret;
	}

	public DBRow getDataRow(String fieldName, String fieldValue) {
		DBRow data = new DBRow();
		DBRow temp = new DBRow();
		try {
			for (int i = 0; i < this.list.size(); i++) {
				temp = (DBRow) this.list.get(i);
				if (temp.getString(fieldName).equals(fieldValue)) {
					data = temp;
				}
			}
		} catch (Exception ex) {
		}

		return data;
	}

	public void addDataSet(DBSet dataSet) {
		dataSet.beforeFirst();
		while (dataSet.next()) {
			this.list.add(dataSet.getDBRow());
		}
	}

	public boolean next() {
		boolean ret = false;
		this.cursor += 1;
		try {
			if (null != this.list.get(this.cursor)) {
				ret = true;
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public boolean previous() {
		boolean ret = false;
		this.cursor -= 1;
		if (this.cursor < 0) {
			ret = true;
		}

		return ret;
	}

	public boolean first() {
		boolean ret = false;
		this.cursor = 0;
		try {
			if (null != this.list.get(this.cursor)) {
				ret = true;
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public boolean last() {
		boolean ret = false;
		this.cursor = (this.list.size() - 1);
		try {
			if (null != this.list.get(this.cursor)) {
				ret = true;
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public void beforeFirst() {
		this.cursor = -1;
	}

	public void afterLast() {
		this.cursor = this.list.size();
	}

	public boolean relative(int rows) {
		boolean ret = false;
		this.cursor = (this.cursor + rows - 1);
		try {
			if (null != this.list.get(this.cursor)) {
				ret = true;
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public boolean absolute(int row) {
		boolean ret = false;
		this.cursor = (row - 1);
		try {
			if (null != this.list.get(this.cursor)) {
				ret = true;
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public int getRow() {
		return (this.cursor + 1);
	}

	public int getRowNum() {
		return this.list.size();
	}

	public boolean isFirst() {
		boolean ret = false;
		if (1 == this.cursor) {
			ret = true;
		}

		return ret;
	}

	public boolean isLast() {
		boolean ret = false;
		if (this.list.size() - 1 == this.cursor) {
			ret = true;
		}

		return ret;
	}

	public boolean isBeforeFirst() {
		boolean ret = false;
		if (-1 == this.cursor) {
			ret = true;
		}

		return ret;
	}

	public boolean isAfterLast() {
		boolean ret = false;
		if (this.list.size() == this.cursor) {
			ret = true;
		}

		return ret;
	}

	public boolean getBoolean(String columnName) {
		boolean ret = false;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getBoolean(columnName);
		} catch (Exception ex) {
		}
		return ret;
	}

	public byte getByte(String columnName) {
		byte ret = 0;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getByte(columnName);
		} catch (Exception ex) {
		}
		return ret;
	}

	public Date getDate(String columnName) {
		Date ret = null;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getDate(columnName);
		} catch (Exception ex) {
		}
		return ret;
	}

	public double getDouble(String columnName) {
		double ret = 0D;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getDouble(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}

	public float getFloat(String columnName) {
		float ret = 0F;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getFloat(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}

	public int getInt(String columnName) {
		int ret = 0;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getInt(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}

	public long getLong(String columnName) {
		long ret = 0L;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getLong(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}

	public short getShort(String columnName) {
		short ret = 0;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getShort(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}

	public String getString(String columnName) {
		String ret = "";
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getString(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		if (ret.toLowerCase().equals("null")) {
			ret = "";
		}

		if ((ret.length() >= 10) && (ret.substring(0, 10).equals("1900-01-01"))) {
			ret = "";
		}

		return ret;
	}

	public byte[] getBytes(String columnName) {
		byte[] ret = null;
		try {
			ret = ((DBRow) this.list.get(this.cursor)).getBytes(columnName
					.toUpperCase());
		} catch (Exception ex) {
		}
		return ret;
	}
}
